<?php

namespace App\Modules\Comment\Jobs;


use App\Library\OSS;
use App\Modules\Comment\Models\Comment;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Overtrue\LaravelWeChat\Facade as EasyWeChat;

class CheckJob implements ShouldQueue
{
    use Queueable,SerializesModels;

    public $comment;

    public function __construct(Comment $comment)
    {
        $this->comment = $comment;
        $this->onQueue('check');
        $this->handle();//队列方法，正式上线需要注释掉
    }

    public function handle()
    {
        $app = EasyWeChat::miniProgram();
        if (!empty($this->comment->image)) {
            foreach ($this->comment->image as $item) {
                $name = tempnam(sys_get_temp_dir(), "check");
                copy(OSS::instance()->getUrl($item), $name);
                $result = $app->content_security->checkImage($name);
                if ($result['errcode'] != 0) {
                    $this->comment->is_checked = 1;
                    $this->comment->save();
                    return ;
                }
            }
        }

        $this->comment->is_show = 1;
        $this->comment->is_checked = 1;
        $this->comment->save();
    }
}
